<template>
  <div>
    <div class="box_table">

      <el-dialog title="查看二级信息" :visible.sync="sceneInfoVisible">
        <div>
          <form_item>
            编号：
            <el-input v-model="columnOne.cnum" placeholder="请输入内容" :disabled="true"></el-input>
            栏目名称：
            <el-input v-model="columnOne.ccname" placeholder="请输入内容" :disabled="true"></el-input>
            创建时间：
            <el-input v-model="columnOne.createDate" placeholder="请输入内容" :disabled="true"></el-input>

          </form_item>

        </div>
      </el-dialog>


      <el-dialog title="添加二级信息" :visible.sync="viewVisible">
        <div>
          <label-wrap>栏目标题：</label-wrap>
          <el-input v-model="columnsList.cCname" placeholder="请输入内容" style="width: 250px"></el-input>
        </div>
        <br>
        <div>
          所属一级栏目字段：
          <template>
            <el-select v-model="columnsList.cCid" placeholder="请选择">
              <el-option
                  v-for="item in brandList"
                  :key="item.id"
                  :label="item.ccname"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </div>
        <br>
        <div>
          请选择二级栏目推荐品牌：
          <el-table
              :data="brand1"
              height="250"
              border
              style="width: 100%">
            <el-table-column
                prop="brandCode"
                label="编号"
                width="303">
            </el-table-column>
            <el-table-column
                prop="brandName"
                label="名称"
                width="303">
            </el-table-column>
            <el-table-column
                label="操作"
                width="303">
              <template slot-scope="scope">
                <el-button type="primary" plain icon="el-icon-minus" circle size="small"
                           @click="delBrand(scope.row)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <br>
        <div>
          <el-form v-model="findBrands">
            品牌编号:
            <el-input v-model="findBrands.brandCode" style="width: 25%"></el-input>
            品牌名称:
            <el-input v-model="findBrands.brandName" style="width: 25%"></el-input>
            <el-button type="primary" plain @click="findBrand()">查找</el-button>
          </el-form>
        </div>
        <div>

          <el-table
              :data="brand"
              height="250"
              border
              style="width: 100%">
            <el-table-column
                prop="brandCode"
                label="编号"
                width="303">
            </el-table-column>
            <el-table-column
                prop="brandName"
                label="名称"
                width="303">
            </el-table-column>
            <el-table-column
                label="是否关联"
                width="303">
              <template slot-scope="scope">
                <el-button type="primary" plain icon="el-icon-plus" circle size="small"
                           @click="addBrand(scope.row)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="quxiao()">取 消</el-button>
          <el-button type="primary" @click="tianjia()">确 定</el-button>
        </div>
      </el-dialog>


      <el-dialog title="修改二级信息" :visible.sync="updateT">

        <div>
          <label-wrap>栏目标题：</label-wrap>
          <el-input v-model="columnsL.ccname" placeholder="请输入内容" style="width: 250px"></el-input>
        </div>
        <br>
        <div>
          所属一级栏目字段：
          <template>
            <el-select v-model="columnsL.ccid" placeholder="请选择">
              <el-option
                  v-for="item in brandList"
                  :key="item.id"
                  :label="item.ccname"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </div>
        <br>
        <div>
          请选择二级栏目推荐品牌：
          <el-table
              :data="brand1ById"
              height="250"
              border
              style="width: 100%">
            <el-table-column
                prop="brandCode"
                label="编号"
                width="303">
            </el-table-column>
            <el-table-column
                prop="brandName"
                label="名称"
                width="303">
            </el-table-column>
            <el-table-column
                label="操作"
                width="303">
              <template slot-scope="scope">
                <el-button type="primary" plain icon="el-icon-minus" circle size="small"
                           @click="updelBrand(scope.row)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <br>
        <div>
          <el-form v-model="findBrands">
            品牌编号:
            <el-input v-model="findBrands.brandCode" style="width: 25%"></el-input>
            品牌名称:
            <el-input v-model="findBrands.brandName" style="width: 25%"></el-input>
            <el-button type="primary" plain @click="findBrandByid()">查找</el-button>
          </el-form>
        </div>
        <div>

          <el-table
              :data="brandById"
              height="250"
              border
              style="width: 100%">
            <el-table-column
                prop="brandCode"
                label="编号"
                width="303">
            </el-table-column>
            <el-table-column
                prop="brandName"
                label="名称"
                width="303">
            </el-table-column>
            <el-table-column
                label="是否关联"
                width="303">
              <template slot-scope="scope">
                <el-button type="primary" plain icon="el-icon-plus" circle size="small"
                           @click="upaddBrand(scope.row)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="quxiao()">取 消</el-button>
          <el-button type="primary" @click="updateColumn()">确 定</el-button>
        </div>
      </el-dialog>


      <el-breadcrumb separator-class="el-icon-arrow-right" style="position:fixed;top: 13%">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商城管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>栏目管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-button type="primary" plain size="medium" @click="addinit()" style="float: right;margin-top: 20px">添加二级栏目
      </el-button>
      <div>
        <el-table
            :data="brandList"
            style="width: 100%;margin-bottom: 20px;"
            height="530"
            row-key="id"
            border
            :tree-props="{children: 'columnList'}">
          <el-table-column
              prop="id"
              label="序号"
              sortable>
          </el-table-column>
          <el-table-column
              prop="cnum"
              label="编号"
              sortable>
          </el-table-column>
          <el-table-column
              prop="ccname"
              label="栏目名称（展开为二级）"
              sortable>
          </el-table-column>
          <el-table-column
              prop="createDate"
              label="添加时间"
              sortable>
          </el-table-column>

          <el-table-column

              label="是否显示"
              sortable>

            <template v-slot="s">
              <span v-if="s.row.ccid ==0">此栏目为父级栏目</span>
              <el-switch
                  @change="alterSwitch(s.row.id,s.row.cdisplay)"
                  v-model="s.row.cdisplay"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  v-if="s.row.ccid !=0"
              >
              </el-switch>
            </template>

          </el-table-column>

          <el-table-column
              label="操作">
            <template slot-scope="scope">
              <span v-if="scope.row.ccid ==0">点开箭头操作子栏目项</span>
              <el-button type="primary" plain icon="el-icon-chat-dot-round" circle size="small"
                         @click="view(scope.row)" v-if="scope.row.ccid !=0"></el-button>
              <el-button type="primary" plain icon="el-icon-edit" circle size="small"
                         @click="updateinit(scope.row)" v-if="scope.row.ccid !=0"></el-button>
              <el-button type="danger" plain icon="el-icon-delete" circle size="small"
                         @click="delColumn(scope.row.id)" v-if="scope.row.ccid !=0"></el-button>
            </template>
          </el-table-column>
        </el-table>


      </div>
    </div>
  </div>
</template>

<script>

export default {
  data() {
    return {
      columnsL: {
        id:"",
        cCname: "",
        cCid: "",
        brands: []
      },
      columnsList: {
        cCname: "",
        cCid: "",
        brands: []
      },
      viewVisible: false,
      brandInfo: {},
      brandList: [],
      brand: [],
      brandById:[],
      brand1: [],
      brand1ById:[],
      sceneInfoVisible: false,
      columnOne: {
        cnum: "",
        ccname: "",
        createDate: ""
      },
      findBrands: {
        brandCode: "",
        brandName: "",
      },
      updateT: false,
    }
  },
  methods: {
    findBrandsByid(id){
      this.$axios({
        method: "get",
        url: "/Column/findBrandsByid",
        params: {
          id: id
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.brand1ById = res.data.data
          this.findBrandByid()
        }
      })


    },

    updateinit(data){
      this.updateT = true
      this.columnsL = data
      this.findBrandsByid(this.columnsL.id)
    },


    findBrandByid(){
      this.$axios({
        method: "post",
        url: "Column/findBrand",
        data: this.findBrands
      }).then(res => {
        this.brandById = []
        let bool;
        if (res.data.code == 200) {
          if (this.brand1ById.length > 0) {
            for (let i = 0; i < res.data.data.length; i++) {
              for (let j = 0; j < this.brand1ById.length; j++) {
                bool = true;
                if (res.data.data[i].id == this.brand1ById[j].id) {
                  bool = false
                }
              }
              if (bool) {
                this.brandById.push(res.data.data[i]);
              }

            }
          } else {
            this.brandById = res.data.data
          }
          console.log(this.brandById)
        }
      })

    },

    updelBrand(data) {
      let index = this.brand1ById.indexOf(data)
      this.brand1ById.splice(index, 1)
      this.brandById.push(data)
    },

    upaddBrand(data) {
      let index = this.brandById.indexOf(data)
      console.log(index)
      this.brandById.splice(index, 1)
      this.brand1ById.push(data)
    },

    delBrand(data) {
      let index = this.brand1.indexOf(data)
      this.brand1.splice(index, 1)
      this.brand.push(data)
    },

    addBrand(data) {
      let index = this.brand.indexOf(data)
      console.log(index)
      this.brand.splice(index, 1)
      this.brand1.push(data)
    },


    //开关方法
    alterSwitch(id, cdisplay) {
      // console.log(this.row)
      //请求方式为post
      this.$axios.post("http://localhost:9527/Column/alterSwitch",
          {
            id: id,
            cDisplay: cdisplay
          }).then(res => {
        if (res.data.code == 200) {
          this.$message({
            type: 'success',
            message: '状态已修改!'
          });
        }
      })
    },

    view(data) {
      this.sceneInfoVisible = true
      console.log(data)
      this.columnOne = data
    },

    delColumn(id) {
      this.$confirm('确定要删除该栏目', {
        confirmButtonText: '确定',
        cancelButtonText: '再想想',
        type: 'warning'
      }).then(() => {
        this.$axios({
          method: "get",
          url: "/Column/delColumn",
          params: {
            id: id
          }
        }).then(res => {
          if (res.data.code == 200) {
            this.sceneInfoVisible = false
            this.loadData()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    updateColumn(){
      this.columnsL.brands = this.brand1ById
      console.log(this.columnsList)
      this.$axios({
        method: "post",
        url: "Column/updateColumn",
        data: this.columnsL

      }).then(res => {
        if (res.data.code == 200) {
          alert("修改成功")
          this.updateT = false
          this.brand1ById = []
          this.columnsL = {}
          this.findBrands = {}
          this.loadData()
        } else {
          alert("修改失败")
          this.updateT = false
          this.loadData()
        }
      })

    },


    tianjia() {
      this.columnsList.brands = this.brand1
      console.log(this.columnsList)
      this.$axios({
        method: "post",
        url: "Column/addErColumn",
        data: this.columnsList

      }).then(res => {
        if (res.data.code == 200) {
          alert("添加成功")
          this.viewVisible = false
          this.brand1 = []
          this.columnsList = {}
          this.findBrands = {}
          this.loadData()
        } else {
          alert("添加失败")
          this.viewVisible = false
          this.loadData()
        }
      })
    },

    quxiao() {
      this.viewVisible = false
      this.columnList = {}
      this.brand1 = []
      this.findBrands = {}

    },

    addinit() {
      this.viewVisible = true,
          this.findBrand()
    },
    findBrand() {
      this.$axios({
        method: "post",
        url: "Column/findBrand",
        data: this.findBrands
      }).then(res => {
        let bool;
        if (res.data.code == 200) {
          if (this.brand1.length > 0) {
            debugger
            for (let i = 0; i < res.data.data.length; i++) {
              for (let j = 0; j < this.brand1.length; j++) {
                bool = true;
                if (res.data.data[i].brandCode == this.brand1[j].brandCode) {
                  bool = false
                }
              }
              if (bool) {
                this.brand.push(res.data.data[i]);
              }

            }
          } else {
            this.brand = res.data.data
          }

        }
      })

    },

    loadData() {
      this.$axios({
        method: "get",
        url: "Column/findOneColumn",
      }).then(res => {
        if (res.data.code == 200) {
          this.brandList = res.data.data

        }
      })
    },


  },
  created() {
    this.loadData()
  }
}
</script>

<style>
.box_table {
  width: 95%;
  margin: 0 auto;
}
</style>
